package com.marisfrolg.storage;

import java.io.File;
import java.lang.ref.WeakReference;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;

public class SettingActivity extends Activity {
	private ProgressDialog progressDialog;
//	private static final String UPDATEURL="http://www.marisfrolg.com/dblist/storagebak.apk";
	private static final String UPDATEOUTTERURL="http://210.75.13.198/dblist/storagebak.apk";
	private static final String UPDATEINNERURL="http://192.168.2.5/dblist/storagebak.apk";
//	private static final String UPDATEURL="http://210.75.13.19/dblist/storagebak.apk";
	private String updateurlNow=UPDATEINNERURL;
	private HttpHandler<File> downHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		ViewUtils.inject(this);
		
	}

	@OnClick(R.id.update)
	public void update(View v){
		if(downHandler!=null){
			downHandler.stop();
		}
		HttpUtils http = new HttpUtils();
		downHandler = http.download(updateurlNow, Environment.getExternalStorageDirectory().getPath()+ "/storage/" + "storagebak.apk", false, false,
				new UpdateCallBack(SettingActivity.this));
		if(UPDATEINNERURL.equals(updateurlNow)){
			updateurlNow=UPDATEOUTTERURL;
		}else{
			updateurlNow=UPDATEINNERURL;
		}
	}
	@OnClick(R.id.pandian)
	public void pandian(View v){
		Intent in=new Intent();
		in.setClass(this,CheckActivity.class);
		startActivity(in);
	}
	
	
	
	private class UpdateCallBack extends RequestCallBack<File> {
		SettingActivity activity;
		UpdateCallBack(SettingActivity activity) {
			this.activity = (new WeakReference<SettingActivity>(activity)).get();
		}
		
		@Override
		public void onLoading(long total, long current, boolean isUploading) {
			int t=(int)total/1000;
			int c=(int)current/1000;
			progressDialog.setMax(t);
			progressDialog.setProgress(c);
			super.onLoading(total, current, isUploading);
		}

		@Override
		public void onStart() {
			progressDialog=new ProgressDialog(activity);
			progressDialog.setTitle("正在下载");
			progressDialog.setCancelable(false);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setMax(0);
			progressDialog.show();
			progressDialog.setOnKeyListener(new OnKeyListener() {
				public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
					if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_UP){
						progressDialog.dismiss();
						try {
							this.finalize();
						} catch (Throwable e) {
							e.printStackTrace();
						}
					}
					return false;
				}
			});
			super.onStart();
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			Toast.makeText(activity, "下载失败", Toast.LENGTH_SHORT).show();
			progressDialog.dismiss();
		}

		@Override
		public void onSuccess(ResponseInfo<File> arg0) {
			File oldFile = new File(Environment.getExternalStorageDirectory().getPath() + "/storage/storage.apk");
			File downFile = new File(Environment.getExternalStorageDirectory().getPath() + "/storage/storagebak.apk");
			if(oldFile.exists()){
				oldFile.delete();
			}
			downFile.renameTo(new File(Environment.getExternalStorageDirectory().getPath() + "/storage/storage.apk"));
			progressDialog.dismiss();
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath() + "/storage/storage.apk")), "application/vnd.android.package-archive");
	        activity.startActivity(intent);
		}

	}
}
